(function($){
    $(document).ready(function() {
        var userAgent = navigator.userAgent.toLowerCase();
        // Figure out what browser is being used
        $.browser = {
            version: (userAgent.match(/.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/) || [])[1],
            chrome: /chrome/.test(userAgent),
            safari: /webkit/.test(userAgent) && !/chrome/.test(userAgent),
            opera: /opera/.test(userAgent),
            msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
            mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent),
            iPad: /iPad/i.test(userAgent),
    		iPhone: /iPhone/i.test(userAgent),
    		iPod: /iPod/i.test(userAgent)
        };
    
        $("#frmRegister").validate({
            rules: {
              CompanyName: "required",    // simple rule, converted to {required: true}
              Address: "required",
              CompanyCityStateZip: "required",
              CompanyPhone: "required",
              BusinessDescription: "required",
              ContactFirstName:"required",
              ContactLastName:"required",              
              Email: {             // compound rule
                required: true, 
                email: true
              },
              Password: {
                required: true,
                minlength: 8
              },
              VerifyPassword: {
                required: true,
                minlength: 8,
                equalTo: "#Password"
              }              
            },
            messages: {
              CompanyName: "*",    // simple rule, converted to {required: true}
              Address: "*",
              CompanyCityStateZip: "*",
              CompanyPhone: "*",
              BusinessDescription: "*",
              ContactFirstName:"*",
              ContactLastName:"*", 
              Email: '*',
              Password: {
                required: "*",
                minlength: "Password must be at least 8 characters"
             },
             VerifyPassword: {
                required: "*",
                minlength: "Password must be at least 8 characters",
                equalTo: "Password fields have to match"
             },
            }
        }); 
        $('#btnResetPass').click(function(){
            pass = $('#new_password').val();
            pass1 = $('#new_password1').val();
            if( pass.length < 8 || pass1.length <8 ) {
                alert("Password at least 8 chars");
                $('#new_password').focus();
                return false;
            } 
            if( pass!=pass1 && pass.length >= 8){
                alert("Password fields have to match");
                $('#new_password1').focus();
                return false;
            }
        });
        //datetiem
        $('#start_date').datepicker({autoSize:false});
        $('#end_date').datepicker({autoSize:false});
        //business listing    
        $('#btnBLAddnew').click(function(){
            window.location = '/business-partners/business-listing/?tab=update';
        });
        $('#btnBLAddnewAdmin').click(function(){
            window.location = '?page=accm&tabmenu=business&tab=update';
        });
         $('#btnCouponAddnew').click(function(){
            window.location = '/business-partners/coupon-management/?tab=update';
        });
        //----------------------
        $.validator.addMethod(
          "selectNone",
          function(value, element) {
            if (element.value == "") {
                element.addClass('error');
                return false;
            }
            else return true;
          },
          "Please select an option."
        );
        //----------------------
        $("#frmBusinessListing").validate();
        $("#btnSubmit").click(function(){           
             $(".required").each(function(){
              $(this).rules("add", {
                required: true,
                email: ($(this).hasClass('email'))?true:false,
                messages: {
                  required: "*"
                }
              });   
            })
        });
        //---------------
        if($.browser.chrome) {
            $('.form-item div input[type="text"]').css('padding','0 5px');
        }
        //check all
        $('#chk_all').click(function(){
            if( this.checked)
                $('.chk_item').attr('checked','true');
            else
                $('.chk_item').removeAttr('checked');
        });
        //delete
        $('#btnDelete').click(function(){
             chk = false;
             $('.chk_item').each(function(){
                if(this.checked) chk = true;
             });
             if (!chk) {
                alert('Please choose item to delete!');
                return false;
             } else {
                return confirm('Are you sure delete!');
             }
        });        
    });    
    //-----------
    function generatePassword() {
        var length = 8,
            charset = "abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
            retVal = "";
        for (var i = 0, n = charset.length; i < length; ++i) {
            retVal += charset.charAt(Math.floor(Math.random() * n));
        }
        return retVal;
    }    
})(jQuery);